package com.ddf.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;


/**
 * @author DDf on 2018/9/21
 * @see META-INF/persistence.xml
 * 双向一对多关系中，必须存在一个关系维护端，在 JPA 规范中，要求  many 的一方作为关系的维护端(owner side), one 的一方作为被维护端(inverse side)。
 * 可以在 one 方指定 @OneToMany 注释并设置 mappedBy 属性，以指定它是这一关联中的被维护端，many 为维护端。
 * 在 many 方指定 @ManyToOne 注释，并使用 @JoinColumn 指定外键名称
 */
@Table(name = "ORDERS")
@Entity
public class Order {
    private Integer id;
    private String name;
    private String description;
    private User user;

    public Order() {

    }

    public Order(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Order(User user, String name, String description) {
        this.user = user;
        this.name = name;
        this.description = description;
    }


    @Id
    @GeneratedValue
    @GenericGenerator(name="increment", strategy = "increment")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "DESCRIPTION")
    public String getDescription() {
        return description;
    }


    public void setDescription(String description) {
        this.description = description;
    }

    @JoinColumn(name = "USER_ID")
    @ManyToOne(targetEntity = User.class)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}
